package com.github.ecbp.store.service.controller.front;

import com.github.ecbp.common.api.CommonResult;
import com.github.ecbp.common.data.builder.CommonPageBuilder;
import com.github.ecbp.common.data.controller.BaseController;
import com.github.ecbp.common.data.utils.TransformUtils;
import com.github.ecbp.common.req.BaseIdDTO;
import com.github.ecbp.common.req.BaseIdListDTO;
import com.github.ecbp.common.req.BaseIdStatusDTO;
import com.github.ecbp.common.utils.Asserts;
import com.github.ecbp.store.service.common.StoreCodeAndMessageEnum;
import com.github.ecbp.store.service.dto.BrandDTO;
import com.github.ecbp.store.service.dto.BrandQueryDTO;
import com.github.ecbp.store.service.entity.Brand;
import com.github.ecbp.store.service.service.BrandService;
import com.github.ecbp.store.service.vo.BrandVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.github.ecbp.common.constant.CacheKeyConstant.PROJECT_NAME;

/**
 * @Author Created by Alex·He on 2020/4/2 23:48
 * @Version V1.0
 * @Description: 品牌表(Brand)表控制层
 */
@Api(tags = "品牌管理")
@RequestMapping("/api/" + PROJECT_NAME + "/brand")
@RestController
public class BrandController extends BaseController {
    @Autowired
    private BrandService service;

    @ApiOperation(value = "新增品牌")
    @PostMapping(value = "/create")
    public CommonResult create(@Validated @RequestBody BrandDTO param) {
        service.saveBase(param);
        return CommonResult.succeed();
    }

    @ApiOperation(value = "根据ID删除品牌")
    @PostMapping(value = "/delete")
    public CommonResult delete(@Validated(BaseIdDTO.NotNullId.class) @RequestBody BaseIdDTO param) {
        Asserts.isTrue(service.removeById(param.getId()), StoreCodeAndMessageEnum.BRAND_DELETE_ERROR);
        return CommonResult.succeed();
    }

    @ApiOperation(value = "批量删除品牌")
    @PostMapping("/deleteByBatch")
    public CommonResult deleteByBatch(@Validated @RequestBody BaseIdListDTO param) {
        Asserts.isTrue(service.removeByIds(param.getIds()), StoreCodeAndMessageEnum.BRAND_DELETE_ERROR);
        return CommonResult.succeed();
    }

    @ApiOperation(value = "修改品牌")
    @PostMapping(value = "/update")
    public CommonResult update(@Validated(BaseIdDTO.NotNullId.class) @RequestBody BrandDTO param) {
        return CommonResult.succeed(service.updateByIdBase(param));
    }

    @ApiOperation(value = "修改品牌显示状态")
    @PostMapping(value = "/updateShowStatus")
    public CommonResult updateShowStatus(@Validated @RequestBody BaseIdStatusDTO param) {
        return CommonResult.succeed(service.updateShowStatus(param.getId(), param.getStatus()));
    }

    @ApiOperation(value = "根据ID获取品牌详情", response = BrandVO.class)
    @PostMapping(value = "/info")
    public CommonResult getItem(@Validated(BaseIdDTO.NotNullId.class) @RequestBody BaseIdDTO param) {
        return CommonResult.succeed(TransformUtils.copy(service.getById(param.getId()), BrandVO.class));
    }

    @ApiOperation(value = "获取全部品牌列表")
    @PostMapping("/listAll")
    public CommonResult listAll() {
        return CommonResult.succeed(service.list());
    }

    @ApiOperation(value = "分页列表-品牌", response = BrandVO.class)
    @PostMapping(value = "/listAllPage")
    public CommonResult listAllPage(@Validated @RequestBody BrandQueryDTO param) {
        return CommonResult.succeed(CommonPageBuilder.build(service.page(TransformUtils.copy(param, Brand.class)), BrandVO.class));
    }

}
